package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;

import com.ccl.iot.R;
import com.ccl.iot.batch.IOTBatch;
import com.ccl.iot.ir.BlirConst;
import com.ccl.iot.publics.ClassManager;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;

public abstract class DeviceIconProvider {
    public static final int KICON_STYLE_DEFAULT = 0;
    public static final int KICON_STYLE_CIRCLE = 0;
    public static final int KICON_STYLE_RECT = 1;

    public static int ALL_ICON_ID = 0;

    //	public static final int[] ICON_LIST_SWITCH_OFF = {R.drawable.icon_spot_light_off, R.drawable.icon_chandelier_off, R.drawable.icon_sofa_lamp_off, R.drawable.icon_breathing_off, R.drawable.icon_ceiling_off, R.drawable.icon_platfond_off};
    //	public static final int[] ICON_LIST_SWITCH_ON = {R.drawable.icon_spot_light_on, R.drawable.icon_chandelier_on, R.drawable.icon_sofa_lamp_on, R.drawable.icon_breathing_on, R.drawable.icon_ceiling_on, R.drawable.icon_platfond_on};


    public static final int[] ICON_LIST_SWITCH_OFF = {
            R.drawable.icon_breathing_off, R.drawable.icon_downlight_off,
            R.drawable.icon_chandelier_off, R.drawable.icon_platfond_off,

            R.drawable.icon_sofa_lamp_off, R.drawable.icon_spot_a_off,
            R.drawable.icon_back_light_off, R.drawable.icon_night_off,

            R.drawable.icon_spot_b_off, R.drawable.icon_strip_off,
            R.drawable.icon_table_off,R.drawable.icon_ventilator_off};

    public static final int[] ICON_LIST_SWITCH_ON = {
            R.drawable.icon_breathing_on, R.drawable.icon_downlight_on,
            R.drawable.icon_chandelier_on, R.drawable.icon_platfond_on,

            R.drawable.icon_sofa_lamp_on, R.drawable.icon_spot_a_on,
            R.drawable.icon_back_light_on, R.drawable.icon_night_on,

            R.drawable.icon_spot_b_on, R.drawable.icon_strip_on,
            R.drawable.icon_table_on,R.drawable.icon_ventilator_on};

    public static final int[] ICON_LIST_SECURITY_OFF = {
            R.drawable.icon_security_disarm, R.drawable.icon_security_b_disarm};

    public static final int[] ICON_LIST_SECURITY_ON = {
            R.drawable.icon_security_arm, R.drawable.icon_security_b_arm};

    //    public static final int[] ICON_LIST_SWITCH_PRESS = {Color.RED, Color.BLACK, Color.BLUE, Color.RED, Color.BLACK, Color.BLUE};

    public static final int[] ICON_LIST_DOOR_CLOSE = {R.drawable.icon_curtain_b_close,
            R.drawable.icon_curtain_c_close, R.drawable.icon_curtain_d_close, R.drawable.icon_curtain_a_close};
    public static final int[] ICON_LIST_DOOR_STOP = {R.drawable.icon_curtain_b_stop,
            R.drawable.icon_curtain_c_stop, R.drawable.icon_curtain_d_stop, R.drawable.icon_curtain_a_stop};
    public static final int[] ICON_LIST_DOOR_OPEN = {R.drawable.icon_curtain_b_open,
            R.drawable.icon_curtain_c_open, R.drawable.icon_curtain_d_open, R.drawable.icon_curtain_a_open};




    public static final int[] ICON_LIST_AIR_OPEN = {R.drawable.icon_air_open};
    public static final int[] ICON_LIST_AIR_CLOSE = {R.drawable.icon_air_close};




    public static final int[] ICON_SECURITY_SELECTED_CLOSE = {R.drawable.icon_security_c_close_sel,
            R.drawable.icon_security_close_sel, R.drawable.icon_security_b_close_sel};
    public static final int[] ICON_SECURITY_SELECTED_OPEN = {R.drawable.icon_security_c_open_sel,
            R.drawable.icon_security_open_sel, R.drawable.icon_security_b_open_sel};
    public static final int[] ICON_SECURITY_SELECTED_ALARM = {R.drawable.icon_security_c_alarm_sel,
            R.drawable.icon_security_alarm_sel, R.drawable.icon_security_b_alarm_sel};





    public enum ICON_ID {
        ;
        public static final int FIRST_ICON = 0;
        public static final int SECOND_ICON = 1;
        public static final int THREE_ICON = 2;
        public static final int FOUR_ICON = 3;
        public static final int FIVE_ICON = 4;
        public static final int SIX_ICON = 5;

        public static final int SEVEN_ICON = 6;
        public static final int EIGHT_ICON = 7;
        public static final int NINE_ICON = 8;
        public static final int TEN_ICON = 9;
        public static final int ELEVEN_ICON = 10;
    }

    public static int getProbeImageId(int index) {
        int id;
        switch (index) {
            case 0:
                id = R.drawable.probe_icon0;
                return id;
            case 1:
                id = R.drawable.probe_icon1;
                return id;
            default:
                id = R.drawable.probe_icon0;
                return id;
        }
    }

    public static int getProbeSelectorId(int index) {
        int id;
        switch (index) {
            case 0:
                id = R.drawable.selector_probe_icon0;
                return id;
            case 1:
                id = R.drawable.selector_probe_icon1;
                return id;
            default:
                id = R.drawable.selector_probe_icon0;
                return id;
        }
    }

    public static int getBlirSelectorId(String type) {
        int id;
        if (BlirConst.IR_SUBDEV_TYPE_TV.equals(type)) {
            id = R.drawable.selector_blir_tv;
        } else if (BlirConst.IR_SUBDEV_TYPE_STB.equals(type)) {
            id = R.drawable.selector_blir_stb;
        } else if (BlirConst.IR_SUBDEV_TYPE_PLAYER.equals(type)) {
            id = R.drawable.selector_blir_player;
        } else if (BlirConst.IR_SUBDEV_TYPE_PROJECTOR.equals(type)) {
            id = R.drawable.selector_blir_projector;
        } else if (BlirConst.IR_SUBDEV_TYPE_OTHER.equals(type)) {
            id = R.drawable.selector_blir_custom;
        } else if (BlirConst.IR_SUBDEV_TYPE_AIRCONDITION.equals(type)) {
            id = R.drawable.selector_blir_air;
        } else if (BlirConst.IR_SUBDEV_TYPE_NON_STATE_AIRCONDITION.equals(type)) {
            id = R.drawable.selector_blir_air;
        } else if (BlirConst.IR_SUBDEV_TYPE_FAN.equals(type)) {
            id = R.drawable.selector_blir_fan;
        } else if (BlirConst.IR_SUBDEV_TYPE_BOX.equals(type)) {
            id = R.drawable.selector_blir_box;
        } else if (BlirConst.IR_SUBDEV_TYPE_AMPLIFIER.equals(type)) {
            id = R.drawable.selector_blir_pa;
        } else if (BlirConst.IR_SUBDEV_TYPE_AIRPURIFIER.equals(type)) {
            id = R.drawable.selector_blir_air_cleaner;
        } else if (BlirConst.IR_SUBDEV_TYPE_WATER_HEATER.equals(type)) {
            id = R.drawable.selector_blir_heater;
        } else {
            id = R.drawable.selector_blir_tv;
        }
        return id;
    }

    public static int getBlirNormalId(String type) {
        int id;
        if (BlirConst.IR_SUBDEV_TYPE_TV.equals(type)) {
            id = R.drawable.icon_tv_open;
        } else if (BlirConst.IR_SUBDEV_TYPE_STB.equals(type)) {
            id = R.drawable.icon_set_box_open;
        } else if (BlirConst.IR_SUBDEV_TYPE_PLAYER.equals(type)) {
            id = R.drawable.icon_dvd_open;
        } else if (BlirConst.IR_SUBDEV_TYPE_PROJECTOR.equals(type)) {
            id = R.drawable.icon_projector_open;
        } else if (BlirConst.IR_SUBDEV_TYPE_OTHER.equals(type)) {
            id = R.drawable.icon_customize;
        } else if (BlirConst.IR_SUBDEV_TYPE_AIRCONDITION.equals(type)) {
            id = R.drawable.icon_air_open;
        } else if (BlirConst.IR_SUBDEV_TYPE_NON_STATE_AIRCONDITION.equals(type)) {
            id = R.drawable.icon_air_open;
        } else if (BlirConst.IR_SUBDEV_TYPE_FAN.equals(type)) {
            id = R.drawable.icon_fan_default;
        } else if (BlirConst.IR_SUBDEV_TYPE_BOX.equals(type)) {
            id = R.drawable.icon_network_boxr_default;
        } else if (BlirConst.IR_SUBDEV_TYPE_AMPLIFIER.equals(type)) {
            id = R.drawable.icon_attack_release_default;
        } else if (BlirConst.IR_SUBDEV_TYPE_AIRPURIFIER.equals(type)) {
            id = R.drawable.icon_air_clear_default;
        } else if (BlirConst.IR_SUBDEV_TYPE_WATER_HEATER.equals(type)) {
            id = R.drawable.icon_heater_default;
        } else {
            id = R.drawable.icon_tv_open;
        }
        return id;
    }




    public static int getSecuritySelectedImageId(int stateIndex, int styleIndex) {
        int id;
        if (styleIndex >= ICON_SECURITY_SELECTED_CLOSE.length) {
            styleIndex = 0;
        }

        switch (stateIndex) {
            case 0:
                id = ICON_SECURITY_SELECTED_CLOSE[styleIndex];
                break;
            case 1:
                id = ICON_SECURITY_SELECTED_ALARM[styleIndex];
                break;
            case 2:
                id = ICON_SECURITY_SELECTED_OPEN[styleIndex];
                break;
            default:
                id = ICON_SECURITY_SELECTED_CLOSE[styleIndex];
                break;
        }

        return id;
    }

    /*
     * 	Static functions
     */
    private static ArrayList<Class<?>> gProviderTypes = null;
    private static HashMap<IOTDevice, DeviceIconProvider> gProviders = null;

    public static boolean RegisterProviderType(Class<?> aType) {
        if (ClassManager.IsSubClass(DeviceIconProvider.class, aType)) {
            if (gProviderTypes == null) {
                gProviderTypes = new ArrayList<Class<?>>();
            } else if (gProviderTypes.contains(aType)) {
                return true;
            }

            return gProviderTypes.add(aType);
        }

        return false;
    }

    public static int GetProviderType(Class<?> aType) {
        if (gProviderTypes != null) {
            return gProviderTypes.indexOf(aType);
        } else {
            return -1;
        }
    }

    public static DeviceIconProvider BuildProvider(IOTDevice aDevice) {
        if (aDevice != null) {
            DeviceIconProvider tProvider = null;

            if (gProviders == null) {
                gProviders = new HashMap<IOTDevice, DeviceIconProvider>();
            } else {
                synchronized (gProviders) {
                    tProvider = gProviders.get(aDevice);
                }
            }

            if (tProvider == null) {
                if (gProviderTypes != null) {
                    for (int i = 0, tCount = gProviderTypes.size(); i < tCount; i++) {
                        Class<?> tProviderType = gProviderTypes.get(i);

                        if (tProviderType != null) {
                            Constructor<?> tConstructor;

                            if ((tConstructor = ClassManager.FindConstructor(tProviderType, aDevice)) != null) {
                                try {
                                    tProvider = (DeviceIconProvider) tConstructor.newInstance(aDevice);
                                } catch (Exception e) {

                                }

                                if (tProvider != null) {
                                    gProviders.put(aDevice, tProvider);

                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return tProvider;
        }

        return null;
    }

    public static Bitmap BuildBitmap(IOTDevice device, int aStyle, int width, int height, int backColor, boolean isPressing) {
        if (device != null) {

            // TODO 这里提供意向控制界面中的批处理图标
            //==========================modify start===========================================
            if (device instanceof BatchIntentionDevice) {
                IOTBatch batch = ((BatchIntentionDevice) device).getBatch();
                return BatchIconProvider.Build(batch, Color.TRANSPARENT, width, height);
            }

            //==========================modify end===========================================

            DeviceIconProvider tProvider = BuildProvider(device);
            //            LoggerUtil.i("名字：----" + tProvider.getClass().getName());

            if (tProvider != null) {
                return tProvider.build(aStyle, width, height, backColor, isPressing);
            }
        }

        return null;
    }

    public static void DrawIconBase(Canvas aCanvas, int aStyle, int aBackColor, int aHighLightColor) {
        if (aCanvas != null) {
            Paint tPaint = new Paint();
            int tWidth = aCanvas.getWidth();
            int tHeight = aCanvas.getHeight();
            float tRefLen = 0, tCenterX = 0, tCenterY = 0, tXOffset = 0, tYOffset = 0;

            if (tWidth > tHeight) {
                tCenterY = tRefLen = tHeight * 0.5f;
                tXOffset = (tWidth - tHeight) * 0.5f;
                tCenterX = tCenterY + tXOffset;
                tWidth = tHeight;
            } else {
                tCenterX = tRefLen = tWidth * 0.5f;
                tYOffset = ((tHeight - tWidth) * 0.5f);
                tCenterY = tCenterX + tYOffset;
                tHeight = tWidth;
            }

            if (aBackColor != Color.TRANSPARENT) {
                aCanvas.drawColor(aBackColor);
            }

            tPaint.setAntiAlias(true);
            tPaint.setFilterBitmap(true);

            tPaint.setColor(aHighLightColor);
            tPaint.setStyle(Paint.Style.FILL);

            if (aStyle == KICON_STYLE_RECT) {
                aCanvas.drawRect(tXOffset, tYOffset, tXOffset + tWidth, tYOffset + tHeight, tPaint);
            } else {
                aCanvas.drawCircle(tCenterX, tCenterY, tRefLen, tPaint);
            }

            RadialGradient tGradient = new RadialGradient(tCenterX, tCenterY, tRefLen * 0.95f, new int[]{Color.WHITE, Color.WHITE, 0xffeeeeee, 0xffdddddd, Color.LTGRAY, 0xffbbbbbb, 0xffaaaaaa}, null, Shader.TileMode.CLAMP);
            tPaint.setShader(tGradient);

            if (aStyle == KICON_STYLE_RECT) {
                float tFrame = tRefLen * 0.05f;

                tXOffset += tFrame;
                tYOffset += tFrame;
                tFrame *= 2;
                aCanvas.drawRect(tXOffset, tYOffset, tXOffset + tWidth - tFrame, tYOffset + tHeight - tFrame, tPaint);
            } else {
                aCanvas.drawCircle(tCenterX, tCenterY, tRefLen * 0.95f, tPaint);
            }
        }
    }

    /*
     * 	Non-static function
     */
    protected IOTDevice iDevice;

    public DeviceIconProvider(IOTDevice aDevice) {
        iDevice = aDevice;
    }

    public abstract Bitmap build(int aStyle, int aWidth, int aHeight, int aBackColor, boolean aPressing);
}
